import { IPublisher, EnterOptionParser } from "../IPublisher";

export class WXPublisher extends IPublisher {
    protected _wx:any;
    private _appid:string;

    initSDK(caller?: any, onSuccess?: Function, onFail?: Function): void {
        this._wx = window['wx'];
        super.initSDK(caller, onSuccess, onFail);
    }

    get appVersion(): string {
        return this._wx.getAccountInfoSync().miniProgram.version;
    }
    
    get appId(): string {
        if (this._appid) {
            return this._appid;
        }
        this._appid = this._wx.getAccountInfoSync().miniProgram.appId;
        return this._appid
    }

    protected _initEnterOptions(info:any): EnterOptionParser {
        if (info) {
            return this._enterOptions = new EnterOptionParser(info);
        }
        if (this._wx.getEnterOptionsSync){
            return this._enterOptions = new EnterOptionParser(this._wx.getEnterOptionsSync());
        }
        return this._enterOptions = new EnterOptionParser("");
    }

    async subscribeMessage(subscribeMsgIds:string[]):Promise<boolean> {
        return this._subscribeMessage(subscribeMsgIds, this._wx);
    }

    onError(call:(...error:any[])=>void):void {
        this._wx.onError(call);
    }

    offError(call:(...error:any[])=>void):void {
        this._wx.offError(call);
    }
}
